package com.liuxvru.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuxvru.common.utils.SecurityUtils;
import com.liuxvru.project.domain.Test;
import com.liuxvru.project.domain.TestStep;
import com.liuxvru.project.mapper.TestMapper;
import com.liuxvru.project.mapper.TestStepMapper;
import com.liuxvru.project.service.ITestStepService;
import org.checkerframework.checker.units.qual.Temperature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 测试明细Service业务层处理
 *
 * @author liuxvru
 * @date 2024-11-22
 */
@Service
public class TestStepServiceImpl extends ServiceImpl<TestStepMapper, TestStep> implements ITestStepService {
    @Autowired
    private TestMapper testMapper;

    /**
     * 查询测试明细列表
     *
     * @param testStep 测试明细
     * @return 测试明细
     */
    @Override
    public List<TestStep> selectTestStepList(TestStep testStep) {
        return baseMapper.selectTestStepList(testStep);
    }

    @Override
    public int add(Long testId, TestStep testStep) {
        Test test = testMapper.selectById(testId);
        if(test==null)throw new RuntimeException("测试不存在");
        Long userId = SecurityUtils.getUserId();
        testStep.setCreatedBy(userId);
        testStep.setUpdatedBy(userId);
        testStep.setTestId(testId);
        testStep.setTestPlanId(test.getTestPlanId());
        return baseMapper.insert(testStep);
    }

    @Override
    public int edit(Long testId, TestStep testStep) {
        Long userId = SecurityUtils.getUserId();
        testStep.setUpdatedBy(userId);
        testStep.setTestId(testId);
        return baseMapper.insert(testStep);
    }

    @Override
    @Temperature
    public int batchEdit(List<TestStep> testSteps) {
        if(testSteps==null|| testSteps.isEmpty()) return 1;
        for (TestStep testStep : testSteps) {
            Long userId = SecurityUtils.getUserId();
            testStep.setUpdatedBy(userId);
        }
        boolean b = this.updateBatchById(testSteps);
        return b? 1 : 0;
    }

}
